package com.xing.servlet;

import com.xing.service.BookService;
import com.xing.service.impl.BookServiceImpl;
import com.xing.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 * 静，谦虚谨慎，心胸宽广容纳所有好的坏的、先苦后甜
 * day41bookshop
 * 2021/5/6 17:02
 */
@WebServlet(value = "/delete")
public class DeleteServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        if(StringUtils.isEmptry(id)){
            resp.sendRedirect(req.getContextPath()+"/booklists");
            return;
        }
        BookService bookService = new BookServiceImpl();
        try {
            int bookId = Integer.parseInt(id);
            bookService.remove(bookId);
            resp.sendRedirect(req.getContextPath()+"/booklists");
        } catch (NumberFormatException e) {
            e.printStackTrace();
            req.setAttribute("msg", "删除失败！");
            resp.sendRedirect(req.getContextPath()+"/message.jsp");
        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
